package com.hy.utils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * author: Mr.Du
 * Date: 2022/9/2
 * Time: 11:31
 */
public class ConnectionUtils {
    
    private ThreadLocal<Connection> threadLocal = new ThreadLocal<>();
    
    public Connection getConnection(){
        Connection connection = threadLocal.get();
        if(connection == null){
            try {
                // 从连接池拿连接并绑定到线程
                connection = DruidUtils.getInstance().getConnection();
                // 绑定到当前线程
                threadLocal.set(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    
    }
}
